package com.yehebl.orm.data.common;

import com.yehebl.orm.data.common.dto.SimplePage;
import com.yehebl.orm.dto.ClassInfo;
import com.yehebl.orm.dto.FieldInfo;
import com.yehebl.orm.exception.ClassNotEntityException;
import com.yehebl.orm.exception.OrmQueryManyException;
import com.yehebl.orm.manager.MethodAccessManager;
import com.yehebl.orm.manager.ReadClassManager;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.time.*;
import java.util.*;
import java.util.Map.Entry;

/**
 * ResultSet组织成实体类
 *
 * @author tzd
 */
public class ResultSetManager {
    /**
     * 获取列名
     */
    private static String[] getColumnNames(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        String[] columnNames = new String[count];
        for (int i = 0; i < count; i++) {
            columnNames[i] = rsmd.getColumnLabel(i + 1);
        }
        return columnNames;
    }

    /**
     * 封装单表实体
     */
    public static <T> T toRsEntity(ResultSet rs, ClassInfo ci, Class<T> clazz) throws Exception {
        String[] columnNames = getColumnNames(rs);
        T t = null;
        int result = 0;
        try {
            while (rs.next()) {
                t = clazz.getDeclaredConstructor().newInstance();
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    if (rsGetObj == null) {
                        continue;
                    }
                    FieldInfo fieldInfo;
                    if (!ci.getAllFieldMap().containsKey(columnName)) {
                        if (!ci.getTableFieldMap().containsKey(columnName)) {
                            continue;
                        }
                        fieldInfo = ci.getTableFieldMap().get(columnName);
                    } else {
                        fieldInfo = ci.getAllFieldMap().get(columnName);
                    }
                    if (!rsGetObj.getClass().equals(fieldInfo.getFieldType())) {
                        rsGetObj = convert(fieldInfo.getFieldType(), rsGetObj);
                    }
                    MethodAccessManager.get(ci.getClazz()).invoke(t, "set" + fieldInfo.getUpperFieldName(), rsGetObj);
                }
                result++;
                if (result > 1) {
                    throw new OrmQueryManyException(clazz.getName());
                }
            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return t;
    }

    /**
     * 封装单表实体多对象
     */
    public static <T> T toRsManyEntityBySqlName(ResultSet rs, ClassInfo ci, Class<T> clazz) throws Exception {
        String[] columnNames = getColumnNames(rs);
        T t = null;
        int result = 0;
        try {
            while (rs.next()) {
                t = clazz.getDeclaredConstructor().newInstance();
                Map<String, Object> objMap = new HashMap<>();
                objMap.put("", t);
                for (String columnName : columnNames) {
                    if (columnName.contains(".")) {
                        String[] dtoAlias = columnName.split("\\.");
                        StringBuilder alias = new StringBuilder();
                        for (int j = 0; j < dtoAlias.length - 1; j++) {
                            if (j == 0) {
                                alias.append(dtoAlias[j]);
                            } else {
                                alias.append(".").append(dtoAlias[j]);
                            }
                            if (objMap.containsKey(alias.toString())) {
                                continue;
                            }
                            Object obj = clazz.getDeclaredField(dtoAlias[j]).getType().getDeclaredConstructor().newInstance();
                            objMap.put(alias.toString(), obj);
                        }
                        Object setObj = objMap.get(columnName.substring(0, columnName.lastIndexOf(".")));
                        String fieldName = columnName.substring(columnName.lastIndexOf(".") + 1);
                        Object objValue = rs.getObject(columnName);
                        if (objValue == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        ClassInfo classInfo = getClassInfo(setObj.getClass());
                        if (!classInfo.getAllFieldMap().containsKey(fieldName)) {
                            if (!classInfo.getTableFieldMap().containsKey(fieldName)) {
                                continue;
                            }
                            fieldInfo = classInfo.getTableFieldMap().get(fieldName);
                        } else {
                            fieldInfo = classInfo.getAllFieldMap().get(fieldName);
                        }
                        if (!objValue.getClass().equals(fieldInfo.getFieldType())) {
                            objValue = convert(fieldInfo.getFieldType(), objValue);
                        }
                        MethodAccessManager.get(setObj.getClass()).invoke(setObj, "set" + fieldInfo.getUpperFieldName(),
                                objValue);
                    } else {
                        Object rsGetObj = rs.getObject(columnName);
                        if (rsGetObj == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        if (!ci.getAllFieldMap().containsKey(columnName)) {
                            if (!ci.getTableFieldMap().containsKey(columnName)) {
                                continue;
                            }
                            fieldInfo = ci.getTableFieldMap().get(columnName);
                        } else {
                            fieldInfo = ci.getAllFieldMap().get(columnName);
                        }
                        if (!rsGetObj.getClass().equals(fieldInfo.getFieldType())) {
                            rsGetObj = convert(fieldInfo.getFieldType(), rsGetObj);
                        }
                        MethodAccessManager.get(ci.getClazz()).invoke(t, "set" + fieldInfo.getUpperFieldName(),
                                rsGetObj);
                    }
                }
                for (Entry<String, Object> entity : objMap.entrySet()) {
                    if (entity.getKey().equals("")) {
                        continue;
                    }
                    if (entity.getKey().contains(".")) {
                        String flName = entity.getKey().substring(0, entity.getKey().lastIndexOf("."));
                        String zjName = entity.getKey().substring(entity.getKey().lastIndexOf(".") + 1);
                        String fieldName = zjName.substring(0, 1).toUpperCase() + zjName.substring(1);
                        Object fl = objMap.get(flName);
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    } else {
                        String fieldName = entity.getKey().substring(0, 1).toUpperCase()
                                + entity.getKey().substring(1);
                        Object fl = objMap.get("");
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    }
                }
                result++;
                if (result > 1) {
                    throw new OrmQueryManyException(clazz.getName());
                }
            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return t;
    }

    public static <T> List<T> toRsManyListBySqlName(ResultSet rs, Class<T> clazz) throws Exception {
        List<T> resultList = new ArrayList<>();
        try {
            while (rs.next()) {
                Map<String, Object> objMap = new HashMap<>();
                T b = clazz.getDeclaredConstructor().newInstance();
                objMap.put("", b);
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    if (columnName.contains(".")) {

                        String[] dtoAlias = columnName.split("\\.");
                        StringBuilder alias = new StringBuilder();
                        for (int j = 0; j < dtoAlias.length - 1; j++) {
                            if (j == 0) {
                                alias.append(dtoAlias[j]);
                            } else {
                                alias.append(".").append(dtoAlias[j]);
                            }
                            if (objMap.containsKey(alias.toString())) {
                                continue;
                            }
                            Object obj = clazz.getDeclaredField(dtoAlias[j]).getType().getDeclaredConstructor().newInstance();
                            objMap.put(alias.toString(), obj);
                        }
                        Object setObj = objMap.get(columnName.substring(0, columnName.lastIndexOf(".")));
                        String fieldName = columnName.substring(columnName.lastIndexOf(".") + 1);
                        Object objValue = rs.getObject(columnName);
                        if (objValue == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        ClassInfo classInfo = getClassInfo(setObj.getClass());
                        if (!classInfo.getAllFieldMap().containsKey(fieldName)) {
                            if (!classInfo.getTableFieldMap().containsKey(fieldName)) {
                                continue;
                            }
                            fieldInfo = classInfo.getTableFieldMap().get(fieldName);
                        } else {
                            fieldInfo = classInfo.getAllFieldMap().get(fieldName);
                        }
                        if (!objValue.getClass().equals(fieldInfo.getFieldType())) {
                            objValue = convert(fieldInfo.getFieldType(), objValue);
                        }
                        MethodAccessManager.get(setObj.getClass()).invoke(setObj, "set" + fieldInfo.getUpperFieldName(),
                                objValue);
                    } else {
                        Object objValue = rs.getObject(columnName);
                        if (objValue == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        ClassInfo classInfo = getClassInfo(b.getClass());
                        if (!classInfo.getAllFieldMap().containsKey(columnName)) {
                            if (!classInfo.getTableFieldMap().containsKey(columnName)) {
                                continue;
                            }
                            fieldInfo = classInfo.getTableFieldMap().get(columnName);
                        } else {
                            fieldInfo = classInfo.getAllFieldMap().get(columnName);
                        }
                        if (!objValue.getClass().equals(fieldInfo.getFieldType())) {
                            objValue = convert(fieldInfo.getFieldType(), objValue);
                        }
                        MethodAccessManager.get(clazz).invoke(b, "set" + fieldInfo.getUpperFieldName(), objValue);
                    }

                }
                for (Entry<String, Object> entity : objMap.entrySet()) {
                    if (entity.getKey().equals("")) {
                        continue;
                    }
                    if (entity.getKey().contains(".")) {
                        String flName = entity.getKey().substring(0, entity.getKey().lastIndexOf("."));
                        String zjName = entity.getKey().substring(entity.getKey().lastIndexOf(".") + 1);
                        String fieldName = zjName.substring(0, 1).toUpperCase() + zjName.substring(1);
                        Object fl = objMap.get(flName);
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    } else {
                        String fieldName = entity.getKey().substring(0, 1).toUpperCase()
                                + entity.getKey().substring(1);
                        Object fl = objMap.get("");
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    }
                }
                resultList.add(b);
            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return resultList;
    }

    public static <T> SimplePage<T> toRsManyListBySqlName(ResultSet rs, Class<T> clazz, int size) throws Exception {
        SimplePage<T> sp = new SimplePage<>();
        List<T> resultList = new ArrayList<>();
        sp.setRows(resultList);
        sp.setNext(false);
        try {
            int sizeNum = 0;
            while (rs.next()) {
                sizeNum++;
                if (sizeNum >= size) {
                    sp.setNext(true);
                    break;
                }
                Map<String, Object> objMap = new HashMap<>();
                T b = clazz.getDeclaredConstructor().newInstance();
                objMap.put("", b);
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    if (columnName.contains(".")) {

                        String[] dtoAlias = columnName.split("\\.");
                        StringBuilder alias = new StringBuilder();
                        for (int j = 0; j < dtoAlias.length - 1; j++) {
                            if (j == 0) {
                                alias.append(dtoAlias[j]);
                            } else {
                                alias.append(".").append(dtoAlias[j]);
                            }
                            if (objMap.containsKey(alias.toString())) {
                                continue;
                            }
                            Object obj = clazz.getDeclaredField(dtoAlias[j]).getType().getDeclaredConstructor().newInstance();
                            objMap.put(alias.toString(), obj);
                        }
                        Object setObj = objMap.get(columnName.substring(0, columnName.lastIndexOf(".")));
                        String fieldName = columnName.substring(columnName.lastIndexOf(".") + 1);
                        Object objValue = rs.getObject(columnName);
                        if (objValue == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        ClassInfo classInfo = getClassInfo(setObj.getClass());
                        if (!classInfo.getAllFieldMap().containsKey(fieldName)) {
                            if (!classInfo.getTableFieldMap().containsKey(fieldName)) {
                                continue;
                            }
                            fieldInfo = classInfo.getTableFieldMap().get(fieldName);
                        } else {
                            fieldInfo = classInfo.getAllFieldMap().get(fieldName);
                        }
                        if (!objValue.getClass().equals(fieldInfo.getFieldType())) {
                            objValue = convert(fieldInfo.getFieldType(), objValue);
                        }
                        MethodAccessManager.get(setObj.getClass()).invoke(setObj, "set" + fieldInfo.getUpperFieldName(),
                                objValue);
                    } else {
                        Object objValue = rs.getObject(columnName);
                        if (objValue == null) {
                            continue;
                        }
                        FieldInfo fieldInfo;
                        ClassInfo classInfo = getClassInfo(b.getClass());
                        if (!classInfo.getAllFieldMap().containsKey(columnName)) {
                            if (!classInfo.getTableFieldMap().containsKey(columnName)) {
                                continue;
                            }
                            fieldInfo = classInfo.getTableFieldMap().get(columnName);
                        } else {
                            fieldInfo = classInfo.getAllFieldMap().get(columnName);
                        }
                        if (!objValue.getClass().equals(fieldInfo.getFieldType())) {
                            objValue = convert(fieldInfo.getFieldType(), objValue);
                        }
                        MethodAccessManager.get(clazz).invoke(b, "set" + fieldInfo.getUpperFieldName(), objValue);
                    }

                }
                for (Entry<String, Object> entity : objMap.entrySet()) {
                    if (entity.getKey().equals("")) {
                        continue;
                    }
                    if (entity.getKey().contains(".")) {
                        String flName = entity.getKey().substring(0, entity.getKey().lastIndexOf("."));
                        String zjName = entity.getKey().substring(entity.getKey().lastIndexOf(".") + 1);
                        String fieldName = zjName.substring(0, 1).toUpperCase() + zjName.substring(1);
                        Object fl = objMap.get(flName);
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    } else {
                        String fieldName = entity.getKey().substring(0, 1).toUpperCase()
                                + entity.getKey().substring(1);
                        Object fl = objMap.get("");
                        MethodAccessManager.get(fl.getClass()).invoke(fl, "set" + fieldName, entity.getValue());
                    }
                }
                resultList.add(b);
            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return sp;
    }

    public static Integer getInteger(ResultSet rs) throws SQLException {
        if (rs.next()) {
            return rs.getInt(1);
        }
        return null;
    }

    public static Integer getInt(ResultSet rs) throws SQLException {
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    }

    public static Object getObj(ResultSet rs) throws SQLException {
        if (rs.next()) {
            return rs.getObject(1);
        }
        return null;
    }

    /**
     * 封装单表List实体
     *
     */
    public static <T> List<T> toRsList(ResultSet rs, ClassInfo classInfo, Class<T> clazz) throws Exception {
        List<T> resultList = new ArrayList<>();
        try {
            while (rs.next()) {
                T t;
                try {
                    t = clazz.getDeclaredConstructor().newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    if (rsGetObj == null) {
                        continue;
                    }
                    FieldInfo fieldInfo;
                    if (!classInfo.getAllFieldMap().containsKey(columnName)) {
                        if (!classInfo.getTableFieldMap().containsKey(columnName)) {
                            continue;
                        }
                        fieldInfo = classInfo.getTableFieldMap().get(columnName);
                    } else {
                        fieldInfo = classInfo.getAllFieldMap().get(columnName);
                    }
                    if (!rsGetObj.getClass().equals(fieldInfo.getFieldType())) {
                        rsGetObj = convert(fieldInfo.getFieldType(), rsGetObj);
                    }
                    MethodAccessManager.get(classInfo.getClazz()).invoke(t, "set" + fieldInfo.getUpperFieldName(),
                            rsGetObj);
                }
                resultList.add(t);
            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return resultList;

    }

    /**
     * 封装单表List实体
     *
     */
    public static <T> SimplePage<T> toRsList(ResultSet rs, ClassInfo classInfo, Class<T> clazz, int size)
            throws Exception {
        SimplePage<T> sp = new SimplePage<>();
        List<T> resultList = new ArrayList<>();
        sp.setRows(resultList);
        sp.setNext(false);
        try {
            int sizeNum = 0;
            while (rs.next()) {
                sizeNum++;
                if (sizeNum >= size) {
                    sp.setNext(true);
                    break;
                }
                T t;
                try {
                    t = clazz.getDeclaredConstructor().newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    if (rsGetObj == null) {
                        continue;
                    }
                    FieldInfo fieldInfo;
                    if (!classInfo.getAllFieldMap().containsKey(columnName)) {
                        if (!classInfo.getTableFieldMap().containsKey(columnName)) {
                            continue;
                        }
                        fieldInfo = classInfo.getTableFieldMap().get(columnName);
                    } else {
                        fieldInfo = classInfo.getAllFieldMap().get(columnName);
                    }
                    if (!rsGetObj.getClass().equals(fieldInfo.getFieldType())) {
                        rsGetObj = convert(fieldInfo.getFieldType(), rsGetObj);
                    }
                    MethodAccessManager.get(classInfo.getClazz()).invoke(t, "set" + fieldInfo.getUpperFieldName(),
                            rsGetObj);
                }
                resultList.add(t);

            }
        } catch (Exception e) {
            throw new Exception("组装实体类异常" + e.getMessage());
        }
        return sp;

    }

    public static Object convert(Class<?> clazz, Object obj) {
        if(obj==null){
            return null;
        }
        if(clazz.equals(Boolean.class)){
            if(obj instanceof Boolean){
                return obj;
            }
            if(obj instanceof  Integer){
                return Integer.valueOf(obj + "")==1?true:false;
            }
        }else if (clazz.equals(Integer.class)) {
            if(obj instanceof Boolean){
                return (boolean)obj?1:0;
            }
            return Integer.valueOf(obj + "");
        } else if (clazz.equals(BigDecimal.class)) {
            return BigDecimal.valueOf(Double.parseDouble(obj + ""));
        } else if (clazz.equals(Long.class)) {
            return Long.valueOf(obj + "");
        } else if (clazz.equals(String.class)) {
            return obj + "";
        } else if (clazz.equals(Float.class)) {
            return Float.valueOf(obj + "");
        } else if (clazz.equals(Short.class)) {
            return Short.valueOf(obj + "");
        } else if (clazz.equals(Double.class)) {
            return Double.valueOf(obj + "");
        } else if (clazz.equals(Character.class)) {
            return String.valueOf(obj).charAt(0);
        } else if (clazz.getName().equals("int")) {
            return Integer.valueOf(obj + "");
        } else if (clazz.getName().equals("long")) {
            return Long.valueOf(obj + "");
        } else if (clazz.getName().equals("float")) {
            return Float.valueOf(obj + "");
        } else if (clazz.getName().equals("short")) {
            return Short.valueOf(obj + "");
        } else if (clazz.getName().equals("double")) {
            return Double.valueOf(obj + "");
        } else if (clazz.getName().equals("char")) {
            return String.valueOf(obj).charAt(0);
        } else if (clazz.equals(LocalDateTime.class)) {
            if(obj instanceof  LocalDateTime){
                return obj;
            }
            if(obj instanceof Date){
                return new Date(((Date)obj).getTime()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            }
        } else if (clazz.equals(LocalDate.class)) {
            if(obj instanceof  LocalDate){
                return obj;
            }
            if(obj instanceof Date){
                return new Date(((Date)obj).getTime()).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            }
        } else if (clazz.equals(LocalTime.class)) {
            if(obj instanceof LocalTime){
                return obj;
            }
            if(obj instanceof Date){
                return new Date(((Date)obj).getTime()).toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
            }
        }else if(clazz.equals(Date.class)){
            if(obj instanceof LocalDateTime){
                return Date.from(((LocalDateTime)obj).atZone(ZoneId.systemDefault()).toInstant());
            }
            if(obj instanceof  LocalDate){
                return Date.from(((LocalDate)obj).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            }
        }
        return obj;
    }

    private static ClassInfo getClassInfo(Class<?> clazz) {
        ClassInfo classInfo = ReadClassManager.map.get(clazz.getName());
        if (classInfo == null) {
            throw new ClassNotEntityException(clazz);
        }
        return classInfo;
    }

    /**
     * 封装Map集合单表实体
     */
    public static <T> Map<String, Object> toMapEntity(ResultSet rs) throws Exception {
        String[] columnNames = getColumnNames(rs);
        int result = 0;
        Map<String, Object> maps = new HashMap<>();
        try {
            while (rs.next()) {
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    maps.put(columnName, rsGetObj);
                }
                result++;
                if (result > 1) {
                    throw new OrmQueryManyException("组装Map集合");
                }
            }
        } catch (Exception e) {
            throw new Exception("组装Map集合异常");
        }
        return maps;
    }

    public static <T> List<Map<String, Object>> toMapListBySqlName(ResultSet rs) throws Exception {
        String[] columnNames = getColumnNames(rs);
        List<Map<String, Object>> lists = new ArrayList<>();
        try {
            while (rs.next()) {
                Map<String, Object> maps = new HashMap<>();
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    maps.put(columnName, rsGetObj);
                }
                lists.add(maps);
            }
        } catch (Exception e) {
            throw new Exception("组装Map集合异常");
        }
        return lists;
    }

    public static <T> SimplePage<Map<String, Object>> toMapListBySqlName(ResultSet rs, int size) throws Exception {
        SimplePage<Map<String, Object>> sp = new SimplePage<>();
        String[] columnNames = getColumnNames(rs);
        List<Map<String, Object>> lists = new ArrayList<>();
        sp.setRows(lists);
        sp.setNext(false);
        try {
            int sizeNum = 0;
            while (rs.next()) {
                sizeNum++;
                if (sizeNum >= size) {
                    sp.setNext(true);
                    break;
                }
                Map<String, Object> maps = new HashMap<>();
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    maps.put(columnName, rsGetObj);
                }
                lists.add(maps);
            }
        } catch (Exception e) {
            throw new Exception("组装Map集合异常");
        }
        return sp;
    }

    /**
     * 封装单表MapList实体
     *
     */
    public static <T> List<Map<String, Object>> toRsMapList(ResultSet rs) throws Exception {
        List<Map<String, Object>> resultList = new ArrayList<>();
        try {
            while (rs.next()) {
                Map<String, Object> maps = new HashMap<>();
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    maps.put(columnName, rsGetObj);
                }
                resultList.add(maps);
            }
        } catch (Exception e) {
            throw new Exception("组装Map集合异常");
        }
        return resultList;

    }

    /**
     * 封装单表MapList实体
     *
     */
    public static <T> SimplePage<Map<String, Object>> toRsMapList(ResultSet rs, int size) throws Exception {
        SimplePage<Map<String, Object>> sp = new SimplePage<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        sp.setRows(resultList);
        sp.setNext(false);

        try {
            int sizeNum = 0;
            while (rs.next()) {
                sizeNum++;
                if (sizeNum >= size) {
                    sp.setNext(true);
                    break;
                }
                Map<String, Object> maps = new HashMap<>();
                String[] columnNames = getColumnNames(rs);
                for (String columnName : columnNames) {
                    Object rsGetObj = rs.getObject(columnName);
                    maps.put(columnName, rsGetObj);
                }
                resultList.add(maps);
            }
        } catch (Exception e) {
            throw new Exception("组装Map集合异常");
        }
        return sp;

    }
}
